package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.LoginPermission;
import cn.wolfcode.wolf2w.annotation.UserInfoResolver;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IStrategyRedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private IStrategyRedisService strategyRedisService;


    /**
     * 测试
     * @param id
     * @return
     */
    @GetMapping("/get")
    public Object get(String id){
        return userInfoService.get(id);
    }

    /**
     * 验证手机是否唯一
     * @param phone
     * @return
     */
    @LoginPermission
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        boolean val = userInfoService.checkPhone(phone);
        return !val;
    }

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @LoginPermission
    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        return JsonResult.success();
    }


    /**
     * 注册
     * @param phone
     * @param nickname
     * @param password
     * @param rpassword
     * @param verifyCode
     * @return
     */
    @PostMapping("/regist")
    public Object regist(String phone,String nickname,String password,String rpassword,String verifyCode){
            //接收前端输入框的所有参数
            userInfoService.regist(phone,nickname,password,rpassword,verifyCode);
        //返回jsonResult数据
        return JsonResult.success();
    }

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public Object login(String username,String password){

        //得到user
        UserInfo user = userInfoService.getUser(username,password);

        //获取token
        String token = userInfoRedisService.setRedis(user);

        //将token和user发送到前端
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("user", user);
        return JsonResult.success(map);
    }

    /**
     *  获取当前用户
     * @param
     * @return
     */
    /*@GetMapping("/currentUser")
    @LoginPermission    //贴上注解代表这个方法需要登录拦截
    public Object currentUser(HttpServletRequest request){
        String token = request.getHeader("token");
        UserInfo user = userInfoRedisService.getRedisUser(token);
        return JsonResult.success(user);
    }*/


    /**
     *  获取当前用户
     * @param userInfo
     * @return
     */
    @GetMapping("/currentUser")
    @LoginPermission
    public Object currentUser(@UserInfoResolver UserInfo userInfo){ //贴了该注解才能获取到用户信息
        return JsonResult.success(userInfo);
    }


    /**
     * 获取用户的收藏攻略id集合
     * @param userId
     * @return
     */
    @GetMapping("/strategies/favor")
    public Object favor(String userId){
        List<String> sidList = strategyRedisService.getStatisFavor(userId);
        return JsonResult.success(sidList);
    }


}
